﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace web01
{
    public partial class _Default : System.Web.UI.Page
    {
        protected double _add1;
        protected double _add2;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnNum0_Click(object sender, EventArgs e)
        {
            try
            {
                if ((int)Session["_oper1"] == 1) tbResult.Text = string.Empty;
            }
            catch
            { ;}
            tbResult.Text += ((Button)sender).Text;
            Session["_oper1"] = 0;
        }

        protected void btnsqrt_Click(object sender, EventArgs e)
        {
            _add2 = Convert.ToDouble(tbResult.Text);
            if (_add2 < 0) tbResult.Text = "无效输入";
            else
            {
                _add2 = Math.Sqrt(_add2);
                tbResult.Text = Convert.ToString(_add2);
            }
            Session["_oper1"] = 1;
        }

        protected void btnEvenOperation(object sender, EventArgs e)
        {
            try { Session["_add1"] = Convert.ToDouble(tbResult.Text); }
            catch { Session["_add1"] = 0; }
            tbResult.Text = string.Empty;
            Session["_oper"] = ((Button)sender).Text;
        }

        protected void btnCompute_Click(object sender, EventArgs e)
        {
            try
            {
                _add2 = Convert.ToDouble(tbResult.Text);
            }
            catch
            {
                _add2 = 0;
            }
            _add1 = Convert.ToDouble(Session["_add1"]);
            switch ((String)Session["_oper"])
            {
                case "+": tbResult.Text = (_add1 + _add2).ToString(); break;
                case "-": tbResult.Text = (_add1 - _add2).ToString(); break;
                case "*": tbResult.Text = (_add1 * _add2).ToString(); break;
                case "/": if (_add2 == 0) tbResult.Text = "除数不能为零";
                    else
                        tbResult.Text = (_add1 / _add2).ToString(); break;
                default: break;
            }
            Session["_oper1"] = 1;
        }

        protected void btnremove_Click(object sender, EventArgs e)
        {
            Session["_add1"] = 0;
            tbResult.Text = string.Empty;
        }

        protected void btnallclear_Click(object sender, EventArgs e)
        {
            tbResult.Text = string.Empty;
        }

        protected void btnclear_Click(object sender, EventArgs e)
        {
            if (tbResult.Text.Length <= 0)
                tbResult.Text = "";
            else
                tbResult.Text = tbResult.Text.Substring(0, tbResult.Text.Length - 1);
        }

        protected void btnminus_Click(object sender, EventArgs e)
        {
            _add2 = Convert.ToDouble(tbResult.Text);
            _add2 = -_add2;
            tbResult.Text = Convert.ToString(_add2);
        }

        protected void btnpercent_Click(object sender, EventArgs e)
        {
            _add2 = Convert.ToDouble(tbResult.Text);
            _add2 = _add2 * 100;
            tbResult.Text = Convert.ToString(_add2);
            Session["_oper1"] = 1;
        }

        protected void btncountdown_Click(object sender, EventArgs e)
        {
            _add2 = Convert.ToDouble(tbResult.Text);
            _add2 = 1 / _add2;
            tbResult.Text = Convert.ToString(_add2);
            Session["_oper1"] = 1;
        }

        protected void tbmc_Click(object sender, EventArgs e)
        {
            Session["_add3"] = 0; Session["_oper1"] = 1;
        }

        protected void tbmr_Click(object sender, EventArgs e)
        {
            tbResult.Text = Convert.ToString(Session["_add3"]); Session["_oper1"] = 1;
        }

        protected void tbms_Click(object sender, EventArgs e)
        {
            Session["_add3"] = Convert.ToDouble(tbResult.Text); Session["_oper1"] = 1;
        }

        protected void tbmadd_Click(object sender, EventArgs e)
        {
            _add2 = Convert.ToDouble(Session["_add3"]);
            switch (((Button)sender).Text)
            {
                case "M+": Session["_add3"] = _add2 + Convert.ToDouble(tbResult.Text); break;
                case "M-": Session["_add3"] = _add2 - Convert.ToDouble(tbResult.Text); break;
                default: break;
            } Session["_oper1"] = 1;
        }





    }
}
